<?php
// @formatter:off
/**
 * @file admupdate.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: admupdate.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/users/admupdate.php $
 */
// @formatter:on

require_once __DIR__."/tools.php";

class WCServiceUsersAdmupdate extends WCService {
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_readOnlyConflict = true;

		$this->_requiredParams["params"][] = "user";
		$this->_requiredParams["params"][] = "groups";

		$this->_requiredPermissions["to"][] = WC_PERM_USER_ADMIN;
	}
	//
	// Protected methods.
	protected function runPOST() {
		global $wcUsersHolder;

		$user = $wcUsersHolder->item($this->user);
		if($user && $user->ok()) {
			global $wcDatabaseAccess;

			$db = WCConnection::Instance();
			$dbprefix = $wcDatabaseAccess["prefix"];

			$stmt = null;
			$stmtId = basename(__FILE__)."[adm-update]";
			if($db->has($stmtId)) {
				$stmt = $db->get($stmtId);
			} else {
				$query = "update  {$dbprefix}group_users\n";
				$query .= "set     rel_gu_profile = :profile\n";
				$query .= "where   rel_gu_group	= :gid\n";
				$query .= " and    rel_gu_user  = :uid";

				$stmt = $db->prepare($stmtId, $query);
			}

			$params = array(
				":uid" => $user->id(),
				":gid" => 0,
				":profile" => ""
			);
			foreach($this->groups as $gid => $profile) {
				$params[":gid"] = $gid;
				$params[":profile"] = $profile;
				if($stmt->execute($params)) {
					$this->_count++;
				} else {
					$errorInfo = $stmt->errorInfo();
					$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "DB Error: [{$errorInfo[0]}-{$errorInfo[1]}] {$errorInfo[2]}");
				}
			}
		}
	}
}
